/////////////////////////////////////////////////////////////////////////////
// 2023 -
//open source under Apache License Version 2.0
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <kmhdfs/client/input_stream_impl.h>
#include <kmhdfs/client/ec_policy.h>
#include <kmhdfs/client/striped_block_util.h>
#include <kmhdfs/client/byte_buffer.h>
#include <kmhdfs/client/stripe_reader.h>

namespace Hdfs {

namespace Internal {

class StripedInputStreamImpl : public InputStreamImpl {
public:
    explicit StripedInputStreamImpl(std::shared_ptr<LocatedBlocks> lbs);
    ~StripedInputStreamImpl() override;

    /**
     * To read data from hdfs.
     * @param buf the buffer used to filled.
     * @param size buffer size.
     * @return return the number of bytes filled in the buffer, it may less than size.
     */
    int32_t read(char * buf, int32_t size) override;

    /**
     * To move the file point to the given position.
     * @param pos the given position.
     */
    void seek(int64_t pos) override;

    /**
     * Close the stream.
     */
    void close() override;

    bool createBlockReader(LocatedBlock & block,
                           long offsetInBlock,
                           std::vector<LocatedBlock> & targetBlocks,
                           std::vector<StripeReader::BlockReaderInfo *> & readerInfos,
                           int chunkIndex);
    void closeReader(StripeReader::BlockReaderInfo * readerInfo);

private:
    int64_t getOffsetInBlockGroup();
    int64_t getOffsetInBlockGroup(int64_t pos);
    int32_t getStripedBufOffset(int64_t offsetInBlockGroup);
    int32_t copyToTarget(char * buf, int32_t size);

    void fetchBlockByteRange(std::shared_ptr<LocatedBlock> curBlock, int64_t start, int64_t end, char * buf) override;
    std::shared_ptr<LocatedBlock> getBlockGroupAt(int64_t offset);
    void readOneStripe();
    void seekToBlock(const LocatedBlock & lb);
    void setCurBlock();
    void resetCurStripeBuffer(bool force);
    void closeCurrentBlockReaders();

    bool isLocalNode(const DatanodeInfo * info);
    const DatanodeInfo * choseBestNode(LocatedBlock & lb);

public:
    ByteBuffer * getCurStripeBuf() const { return curStripeBuf; }
    ByteBuffer * getParityBuffer();

private:
    int32_t cellSize;
    int8_t dataBlkNum;
    int8_t parityBlkNum;
    int32_t groupSize;
    std::shared_ptr<ECPolicy> ecPolicy;
    

private:
    std::shared_ptr<StripedBlockUtil::StripeRange> curStripeRange;
    ByteBuffer * curStripeBuf;
    ByteBuffer * parityBuf;
    std::vector<StripeReader::BlockReaderInfo *> blockReaders;
    std::shared_ptr<RawErasureDecoder> decoder;
};


}
}
